﻿$(document).ready(function () {
    $("#AddDapAn").click(function () {
        var MaLoai = $("#MainContent_ddlloaicauhoi").val();
        if (MaLoai == 4) {
            jAlert("Câu hỏi đúng sai, không thể thêm đáp án khác!","Thông báo");
        }
        ThemDapAn();
        return false;
    });

    $("#MainContent_ddlloaicauhoi").change(function () {
        ChangeLoaiCauHoi();
    });
    ChangeLoaiCauHoi();
});
function ChangeLoaiCauHoi() {
    $("#MainContent_txtsoluachon").val("");
    var MaLoai = $("#MainContent_ddlloaicauhoi").val();
    if (MaLoai == 2 || MaLoai == 4)
        $("#MainContent_txtsoluachon").val("1");
    if (MaLoai == 4)
        CauHoiDungSai();
    if (MaLoai == 3) {
        var soluachon = $("#DanhSachDapAn").find("input[type='checkbox']:checked").length;
        $("#MainContent_txtsoluachon").val(soluachon);
    }
}
function TongLuaChon() {
    var MaLoai = $("#MainContent_ddlloaicauhoi").val();
    if (MaLoai == 2 || MaLoai == 4)
        return true;
    var soluachon = $("#DanhSachDapAn").find("input[type='checkbox']:checked").length;
    $("#MainContent_txtsoluachon").val(soluachon);
    return true;
}
function ThemDapAn() {
    var content = '<tr><td style="vertical-align:middle; width:30px"><input type="checkbox" class="GiaTri" onclick="return TongLuaChon();"></td>';
    content += '<td style="vertical-align:middle"><textarea class="form-control NoiDung" rows="3" placeholder="nhập đáp án..."></textarea></td>';
    content += '<td style="vertical-align:middle; width:50px"><button onclick="return XoaDapAn(this);" class="btn btn-default m-l-3" title="Xóa"><i class="fa fa-times"></i> </button></td></tr>';
    $("#DanhSachDapAn").append(content);
}

function CauHoiDungSai() {
    $("#DanhSachDapAn").find("tr").remove();
    var content = '<tr><td style="vertical-align:middle; width:30px"><input type="checkbox" class="GiaTri" onclick="return TongLuaChon();"></td>';
    content += '<td style="vertical-align:middle"><textarea class="form-control NoiDung" rows="3">Đúng</textarea></td>';
    content += '<td style="vertical-align:middle; width:50px"></td></tr>';
    $("#DanhSachDapAn").append(content);

    var content = '<tr><td style="vertical-align:middle; width:30px"><input type="checkbox" class="GiaTri" onclick="return TongLuaChon();"></td>';
    content += '<td style="vertical-align:middle"><textarea class="form-control NoiDung" rows="3">Sai</textarea></td>';
    content += '<td style="vertical-align:middle; width:50px"></td></tr>';
    $("#DanhSachDapAn").append(content);
}

function XoaDapAn(obj) {
    $(obj).parent().parent().remove();
    return false;
}

function checkvaid() {
    var soluachon = $("#DanhSachDapAn").find("input[type='checkbox']:checked").length;
    var cauhoidung = $("#MainContent_txtsoluachon").val();
    var noidung = $("#MainContent_txtnoidungch").val();
    if (soluachon != cauhoidung) {
        jAlert("Bạn phải " + cauhoidung + " đáp án đúng", "Thông báo");
        return false;
    }
    if (noidung == '')
    {
        jAlert("Bạn chưa nhập nội dung", "Thông báo");
        return false;
    }
    var fgdapan = true;
    $("#DanhSachDapAn textarea").each(function (i, o) {
        if (o.value == '') {
            fgdapan = false;
            return false;
        }
    });
    if (fgdapan == false)
    {
        jAlert("Bạn chưa nhập xong đáp án", "Thông báo");
        return false;
    }
    return insert();
}

function insert() {
    var CauHoi = { NoiDungCH: '', MaLoai: 0, MaMon: 0,MaDoKho: 0, SoLuaChon: 1 };
    var dsDapAn = []; 

    $("#DanhSachDapAn tr").each(function (i,o) {
        var giatri = 0;
        if ($(o).find(".GiaTri").is(":checked"))
            giatri = 1;
        var noidung = $(o).find(".NoiDung").val();
        var DapAn = {};
        DapAn.GiaTri = giatri;
        DapAn.NoiDungDA = noidung;
        dsDapAn.push(DapAn);
    });
    
    CauHoi.NoiDungCH = $(".txtnoidungch").val();
    CauHoi.MaMon = $(".ddlmonthi").val();
    CauHoi.MaLoai = $(".ddlloaicauhoi").val();
    CauHoi.MaDoKho = $(".ddldokho").val();
    CauHoi.SoLuaChon = $(".txtsoluachon").val();
    
    $.ajax({
        type: "POST",
        url: "../Ajax/CauHoiAjax.aspx",
        data: { Methods: "AddCauHoi", CauHoi: JSON.stringify(CauHoi), DapAn: JSON.stringify(dsDapAn) },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                jAlert("Thành công", "Thong bao");
                $(".txtnoidungch").val("");
                $(".txtsoluachon").val("");
                $("#DanhSachDapAn").find("tr").remove();
            } else {
                jAlert("Không thể thêm câu hỏi", "Thong bao");
            }
        },
        error: function () {
            jAlert("Loi","Thong bao");
        }
    });

    return false;
}

function checkUpdate() {
    var soluachon = $("#DanhSachDapAn").find("input[type='checkbox']:checked").length;
    var cauhoidung = $("#MainContent_txtsoluachon").val();
    if (soluachon != cauhoidung) {
        jAlert("Lỗi", "Thong bao");
        return false;
    }
    return update();
}

function update() {
    var CauHoi = { MaCH: 0, NoiDungCH: '', MaLoai: 0, MaMon: 0, SoLuaChon: 1, MaDoKho: 0 };
    var dsDapAn = [];

    $("#DanhSachDapAn tr").each(function (i, o) {
        var giatri = 0;
        if ($(o).find(".GiaTri").is(":checked"))
            giatri = 1;
        var noidung = $(o).find(".NoiDung").val();
        var DapAn = {};
        DapAn.GiaTri = giatri;
        DapAn.NoiDungDA = noidung;
        dsDapAn.push(DapAn);
    });

    CauHoi.MaCH = $("#txtMaCauHoi").val();
    CauHoi.NoiDungCH = $(".txtnoidungch").val();
    CauHoi.MaMon = $(".ddlmonthi").val();
    CauHoi.MaLoai = $(".ddlloaicauhoi").val();
    CauHoi.SoLuaChon = $(".txtsoluachon").val();
    CauHoi.MaDoKho = $(".ddldokho").val();

    $.ajax({
        type: "POST",
        url: "../Ajax/CauHoiAjax.aspx",
        data: { Methods: "UpdateCauHoi", CauHoi: JSON.stringify(CauHoi), DapAn: JSON.stringify(dsDapAn) },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                jAlert("Thành công", "Thong bao");
            } else {
                jAlert("Không thể thêm câu hỏi", "Thong bao");
            }
        },
        error: function () {
            jAlert("Loi", "Thong bao");
        }
    });

    return false;
}